package com.xy;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;

public class Producer {
    public static void main(String[] args) throws MQClientException, UnsupportedEncodingException, RemotingException, InterruptedException, MQBrokerException {
        //1.创建一个消息生产者，并设置一个生产者组
        DefaultMQProducer producer = new DefaultMQProducer("xy_producer_group");
        //2.指定namesrv地址
        producer.setNamesrvAddr("47.99.65.138:9876");
        //3.初始化生产者，整个生命周期只需要初始化一次
        producer.start();

        for (int i = 0; i < 100; i++) {
            //创建一条消息
            Message msg = new Message(
                    "topic_example_java",
                    "TagA",
                    ("Hello Jave demo RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
            );
            //发送消息并返回结果
            SendResult sendResult = producer.send(msg);
            System.out.println( sendResult);
        }

        producer.shutdown();
    }
}
